//
//  SEHomeController.m
//  kuaiyi
//
//  Created by Alphts on 16/7/5.
//  Copyright © 2016年 Alphts. All rights reserved.
//

#import "SEHomeController.h"
#import "SEHomeKindView.h"
@interface SEHomeController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, weak) UICollectionView *turnCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, weak) UIView *bottomView;

//button图片数组
@property (nonatomic, strong) NSArray *btnImgStrArray;
@property (nonatomic, strong) NSArray *btnTitleArray;
@property (nonatomic, strong) NSMutableArray *bottomArray;

@end

@implementation SEHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.btnImgStrArray = @[@"zl",@"xyg",@"sj",@"xy",@"gt",@"gy"];
     self.btnTitleArray = @[@"肿瘤",@"心血管",@"神经科",@"血液科",@"骨科",@"公益"];
    
    [self setupUI];

}

#pragma mark - 视图布局完成
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    self.flowLayout.itemSize = self.turnCollectionView.frame.size;
    
    //设置九宫格
    //MARK: 设置九宫格
    NSInteger row = 2;
    NSInteger column = 3;
    CGFloat margin = 2;
    CGFloat width = (_bottomView.w - (column-1)*margin)/column;
    CGFloat height = (_bottomView.h -row*margin)/row;
    for (int i = 0; i < self.btnImgStrArray.count; i++) {
        NSInteger colIndex = i % column;
        NSInteger rowIndex = i / column;
        CGFloat viewX = colIndex * (width + margin);
        CGFloat viewY = rowIndex * height + (rowIndex + 1)*margin;
        
        SEHomeKindView *view = self.bottomArray[i];
        
        view.frame = CGRectMake(viewX, viewY, width, height);

        view.imgName = self.btnImgStrArray[i];
        view.title = self.btnTitleArray[i];
        view.index = i;
    }
    
}


#pragma mark - 设置UI界面
- (void)setupUI{
    
    self.navigationController.navigationBar.barTintColor = kThemeColor;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"马医一生";
    
    //MARK: 创建控件
    //1.头部天气和日期view
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = LIGHTBLUE_COLOR;
    [self.view addSubview:headerView];
    
    //2.轮播的collectionView
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];;
    UICollectionView *turnCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    self.turnCollectionView = turnCollectionView;
    turnCollectionView.delegate = self;
    [self.view addSubview:turnCollectionView];

    //3.名医通的view
    UIView *middleView = [[UIView alloc] init];
    [self.view addSubview:middleView];
    
    //左侧的小view
    UIImageView *smallImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yj"]];
    [middleView addSubview:smallImgView];
    
    //名医通label
    UILabel *lb_doctor = [[UILabel alloc] init];
    lb_doctor.text = @"名医通";
    lb_doctor.font = FONT(20);
    [middleView addSubview:lb_doctor];
    
    //4.底部的九宫格view
    UIView *bottomView = [[UIView alloc] init];
    self.bottomView = bottomView;
    bottomView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:bottomView];
    
    for (int i = 0; i < self.btnImgStrArray.count; i++) {
        SEHomeKindView *view = [[SEHomeKindView alloc] initWithFrame:CGRectZero];
        [_bottomView addSubview:view];
        
        //点击按钮回调的block
        view.kindViewBlock = ^(NSInteger tag){
            
            [self didClickKindButtonWith:tag];
        };
        
        [self.bottomArray addObject:view];
    }
    
    //MARK: 设置约束
    //1.头部天气和日期view
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.mas_topLayoutGuideBottom);
        
        make.trailing.leading.mas_equalTo(self.view);
        
        make.height.mas_equalTo(70);
    }];
    
    //2.轮播的collectionView
    [self.turnCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(headerView.mas_bottom);
        
        make.leading.trailing.mas_equalTo(self.view);
        
        make.height.mas_equalTo(210);
        
    }];
    
    //3.名医通的view
    [middleView makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(turnCollectionView.bottom);
        make.trailing.leading.equalTo(self.view);
        make.height.equalTo(43);
    }];
    
    [smallImgView makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(middleView).offset(10);
        make.centerY.equalTo(middleView);
    }];
    
    [lb_doctor makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(smallImgView.trailing).offset(20);
        make.centerY.equalTo(smallImgView);
    }];
    
    //4.底部的九宫格view
    [bottomView makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(middleView.bottom);
        make.bottom.leading.trailing.equalTo(self.view);
    }];
 
}

#pragma mark - 私有方法
//点击种类按钮执行的方法
- (void)didClickKindButtonWith:(NSInteger)tag{
    
    //很据按钮tag值得不同,跳转到不同的控制器
    
    
}

#pragma mark - 数据源和代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeCell" forIndexPath:indexPath];
    
    
    return cell;
    
}

#pragma mark - lazyload
- (NSMutableArray *)bottomArray{
    if (!_bottomArray) {
        _bottomArray = [NSMutableArray array];
    }
    return _bottomArray;
}



@end









